<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once(APPPATH. 'controllers/admin/ad_layout.php');
/**
 * @author Nguyen van chi (chinguyen.phpdev@gmail.com)
 * @copyright 2014 - OtcSoft
 */
class Admincp extends Ad_layout 
{
	public function __construct()
	{
		parent::__construct();
	}
	
	public function index()
	{
		$config['per_page'] = 15;
		if($this->input->post('search')){
			$search = $this->input->post('search');
			$page = 0;
		}else{
			$search = $this->input->get('search');
		}
		$page = $this->uri->segment(3);
		$result = $this->customers_model->getAllSearch($search, $page, $config['per_page']);
		
		
		$this->load->library('pagination');
		
		$config['base_url'] = base_url().'/admin/khach-hang';
		$config['total_rows'] = $result['total_record'];
		//echo $result['sql'];
		if($search != ''){
			$this->pagination->suffix = '?search='. $search;
		}
		$config['use_page_numbers'] = false;
		$config['uri_segment'] = 3;
		$config['num_links'] = 1;
		$this->pagination->initialize($config);
		$data["paging"] = $this->pagination->create_links();
		$data['tbl_content'] = $result['tbl_content'];
		$this->_data['main_container'] = $this->load->view('admin/admincp.phtml', array(
				'tbl_content' => $data['tbl_content'],
				//'industry' => $data['industry'],
				'pagination' => $data["paging"],
		), true);
		$this->load->view('admin/ad_layout_view.phtml', $this->_data);
	}
	
	public function edit()
	{
		
		if($this->form_validation->run() == FALSE){
			$detail = $this->utility_model->getDetail('tbl_customers', 'c_id',  $this->uri->segment(4));
            @$detailproduct = $this->utility_model->getDetail('tbl_products', 'p_id',  $detail->pid);   
			$this->_data['main_container'] = $this->load->view('admin/admincp_detail.phtml', array(
					'detail' => $detail,
					'detailproduct' => @$detailproduct
			), true);
			$this->load->view('admin/ad_layout_view.phtml', $this->_data);
		}else{
			$this->updated();
		}
	}
	
	public function updated()
	{
		$baseurl = base_url();
		$id = $this->input->post('c_id');
		$where = 'c_id';
		$table = 'tbl_customers';
		$data = array(
				'status' => $this->input->post('status'),
				'updated' => date('Y-m-d H:i:s'),
		);
		$result = $this->utility_model->updated($where, $id, $table, $data);
		if($result){
			echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
			<script>
			alert('Bạn đã cập nhật thành công');
			window.open('{$baseurl}admin/khach-hang/','_self');
			</script>";
		}else {
		log_message('error', 'Some variable did not contain a value.');
		}
	}

	
	
	
}